package com.cskaoyan.service.admin;

import com.cskaoyan.bean.MarketRegion;
import com.cskaoyan.bean.MarketRegionExample;
import com.cskaoyan.mapper.MarketRegionMapper;

import com.cskaoyan.model.admin.vo.BaseDataListVo;
import com.cskaoyan.model.admin.vo.CityRegionVo;
import com.cskaoyan.model.admin.vo.DistrictRegionVo;
import com.cskaoyan.model.admin.vo.ProvinceRegionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author hanshuai
 * @version 1.0
 * @description @TODO
 * @date 2021/11/30 16:13
 */
@Service
public class AdminRegionServiceImpl implements AdminRegionService {
    @Autowired
    MarketRegionMapper marketRegionMapper;

    @Override
    public BaseDataListVo regionList() {

        List<ProvinceRegionVo> provinceList = new ArrayList<>();
        MarketRegionExample example = new MarketRegionExample();
        example.createCriteria().andTypeEqualTo((byte) 1);
        List<MarketRegion> regionsOfProvince = marketRegionMapper.selectByExample(example);
        //省循环
        for (MarketRegion marketRegionOfPrince : regionsOfProvince) {
            MarketRegionExample example1 = new MarketRegionExample();
            example1.createCriteria().andPidEqualTo(marketRegionOfPrince.getId());
            List<CityRegionVo> cityRegionVos = new ArrayList<>();
            List<MarketRegion> cityRegions = marketRegionMapper.selectByExample(example1);
            //市循环
            for (MarketRegion cityRegion : cityRegions) {
                MarketRegionExample example2 = new MarketRegionExample();
                example2.createCriteria().andPidEqualTo(cityRegion.getId());
                List<DistrictRegionVo> districtRegionVos = new ArrayList<>();
                List<MarketRegion> districtRegions = marketRegionMapper.selectByExample(example2);
                for (MarketRegion districtRegion : districtRegions) {
                    DistrictRegionVo districtRegionVo = new DistrictRegionVo();
                    districtRegionVo.setCode(districtRegion.getCode());
                    districtRegionVo.setId(districtRegion.getId());
                    districtRegionVo.setType(districtRegion.getType());
                    districtRegionVo.setName(districtRegion.getName());
                    districtRegionVos.add(districtRegionVo);
                }
                CityRegionVo cityRegionVo = new CityRegionVo();
                cityRegionVo.setType(cityRegion.getType());
                cityRegionVo.setCode(cityRegion.getCode());
                cityRegionVo.setName(cityRegion.getName());
                cityRegionVo.setId(cityRegion.getId());
                cityRegionVo.setChildren(districtRegionVos);
                cityRegionVos.add(cityRegionVo);
            }
            ProvinceRegionVo provinceRegionVo = new ProvinceRegionVo();
            provinceRegionVo.setCode(marketRegionOfPrince.getCode());
            provinceRegionVo.setName(marketRegionOfPrince.getName());
            provinceRegionVo.setType(marketRegionOfPrince.getType());
            provinceRegionVo.setId(marketRegionOfPrince.getId());
            provinceRegionVo.setChildren(cityRegionVos);
            provinceList.add(provinceRegionVo);
        }

        BaseDataListVo baseDataListVo = new BaseDataListVo();
        baseDataListVo.setList(provinceList);
        baseDataListVo.setLimit(31);
        baseDataListVo.setTotal(31);
        baseDataListVo.setPage(1);
        baseDataListVo.setPages(1);
        return baseDataListVo;
    }
}
